package com.yuanqi.repository;

import com.yuanqi.domain.PaymentApproval;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface PaymentApprovalRepository extends JpaRepository<PaymentApproval, Long> {
    
    /**
     * 根据审批编号查找
     */
    Optional<PaymentApproval> findByApprovalCode(String approvalCode);
    
    /**
     * 根据状态查找
     */
    List<PaymentApproval> findByStatus(Integer status);
    
    /**
     * 根据审批类型查找
     */
    List<PaymentApproval> findByApprovalType(String approvalType);
    
    /**
     * 根据申请人查找
     */
    List<PaymentApproval> findByApplicant(String applicant);
    
    /**
     * 根据申请人和状态查找
     */
    List<PaymentApproval> findByApplicantAndStatus(String applicant, Integer status);
    
    /**
     * 根据订单ID查找
     */
    List<PaymentApproval> findByOrderId(Long orderId);
    
    /**
     * 根据订单编号查找
     */
    List<PaymentApproval> findByOrderCode(String orderCode);
    
    /**
     * 根据供应商ID查找
     */
    List<PaymentApproval> findBySupplierId(Long supplierId);
    
    /**
     * 根据客户ID查找
     */
    List<PaymentApproval> findByCustomerId(Long customerId);
    
    /**
     * 根据状态统计数量
     */
    long countByStatus(Integer status);
    
    /**
     * 根据审批类型和状态统计数量
     */
    long countByApprovalTypeAndStatus(String approvalType, Integer status);
    
    /**
     * 查找最大的审批编号数字部分
     */
    @Query("SELECT MAX(CAST(SUBSTRING(pa.approvalCode, 3) AS integer)) FROM PaymentApproval pa WHERE pa.approvalCode LIKE 'PA%'")
    Integer findMaxApprovalCodeNumber();
}
